﻿#include "Game.h"

D3DXVECTOR2 TurretPosition[MAX_TURRET] = {D3DXVECTOR2(39,2), D3DXVECTOR2(51,3), D3DXVECTOR2(57,3), D3DXVECTOR2(93,1), D3DXVECTOR2(97,1)};
D3DXVECTOR2 SuperTurretPosition[MAX_SUPER_TURRET] = {D3DXVECTOR2(64,2), D3DXVECTOR2(68,5), D3DXVECTOR2(86,3)};
D3DXVECTOR3 WeaponCapsulePosition[MAX_WEAPON_CAPSULE] = {D3DXVECTOR3(11,5,5), D3DXVECTOR3(40,5,3), D3DXVECTOR3(76,5,5), D3DXVECTOR3(76,2,4)};

CGame::CGame(void)
{
	_contra = new CContra();
	_background = new CBackground();
	for(int i = 0; i < MAX_TURRET; i++)
		_turret[i] = new CTurret();
	for(int i = 0; i < MAX_SUPER_TURRET; i++)
		_superTurret[i] = new CSuperTurret();
	for(int i = 0; i < MAX_WEAPON_CAPSULE; i++)
		_capsule[i] = new CWeaponCapsule();


	_EnemyWhite = new CEnemy_White();
	_EnemyRed = new CEnemyRed();
}
int countheight1;
int countwidth1;
int MAPDATA12[104][7];

int CGame::Game_Init(HINSTANCE hInstance, HWND hWnd)
{
	HRESULT result;
	DWORD test;
	srand(time(NULL));

	if (!Init_Direct3D(hWnd, SCREEN_WIDTH, SCREEN_HEIGHT, FULLSCREEN))
	{
		MessageBox(hWnd, L"Error initializing Direct3D", L"Error", MB_OK);
		return 0;
	}

	//sound the game
	if (!_sound.Init_DirectSound(hWnd))
	{
		MessageBox(hWnd, L"Error intializing DirectSound", L"Error", MB_OK);
		return 0;
	}
	if (!_contra->ContraLoadSound(_sound))
	{
		MessageBox(hWnd, L"Error Load DirectSound", L"Error", MB_OK);
		return 0;
	}

	D3DXCreateSprite(_d3ddev, &_spriteHandler);

	// function the load image and initializing
	CSpriteManager::getInstance()->LoadSprite(_spriteHandler);
	CExplosionManager::GetInstance()->LoadResources();

	_contra->LoadResource(_d3ddev, _spriteHandler, hInstance, hWnd);
	_background->Background_Init(_d3ddev, _spriteHandler, hInstance, hWnd);
		for(int i = 0; i < MAX_TURRET; i++)
	{
		_turret[i]->LoadResource();
		_turret[i]->SetPosition(TurretPosition[i].x*64 + 32, TurretPosition[i].y*64 + 32);
	}
	for(int i = 0; i < MAX_SUPER_TURRET; i++)
	{
		_superTurret[i]->LoadResource();
		_superTurret[i]->SetPosition(SuperTurretPosition[i].x*64 + 32, SuperTurretPosition[i].y*64 + 32);
	}
	for(int i = 0; i < MAX_WEAPON_CAPSULE; i++)
	{
		_capsule[i]->LoadResource(_d3ddev, _spriteHandler, hInstance, hWnd);
		_capsule[i]->Spawn(D3DXVECTOR2(WeaponCapsulePosition[i].x * 64,WeaponCapsulePosition[i].y * 64 + 32), WeaponCapsulePosition[i].z);
	}


	_EnemyWhite->LoadResource(_d3ddev, _spriteHandler, hInstance, hWnd);
	_EnemyRed->LoadResource(_d3ddev, _spriteHandler, hInstance, hWnd);

	_EnemyRed->SetPosition(634, 96);

	start = GetTickCount();

	return 1;
}

void CGame::Game_Run(HWND hWnd)
{
	if (_d3ddev == NULL)
		return;

	if (GetTickCount() - start > 15)
	{
		start = GetTickCount();
		_contra->Contra_Run();
	}

	_contra->HandleCollisionOnMap();

	if (_d3ddev->BeginScene())
	{
		_d3ddev->Clear(1, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 1, NULL);

		_background->Background_Render(_contra->GetPosition());
		
		for(int i = 0; i < MAX_TURRET; i++)
			_turret[i]->Render(_contra->GetPosition());
		for(int i = 0; i < MAX_SUPER_TURRET; i++)
			_superTurret[i]->Render(_contra->GetPosition());
		for(int i = 0; i < MAX_WEAPON_CAPSULE; i++)
			_capsule[i]->Render(_contra->GetPosition());

		_EnemyWhite->Render(_contra->GetPosition());
		_EnemyRed->Render(_contra->GetPosition());

		CExplosionManager::GetInstance()->Render(_contra->GetPosition());

		_contra->Render();
		_d3ddev->EndScene();
	}

	_d3ddev->Present(NULL, NULL, NULL, NULL);

	if (KEY_DOWN(VK_ESCAPE))
		PostMessage(hWnd, WM_DESTROY, 0, 0);
}

void CGame::Game_End(HWND hWnd)
{
	if (_d3ddev != NULL)
		_d3ddev->Release();

	//release the Direct3D object
	if (_d3d != NULL)
		_d3d->Release();

	if (_backbuffer != NULL)
		_backbuffer->Release();

	_contra->Contra_End();
	_background->Background_End();
}

CGame::~CGame(void)
{
	delete _EnemyWhite;
	delete _EnemyRed;
}